import { queryOperationList } from './service';

//打印类型
const printTypeOptions = [
	{ label: '唯一码', value: 1 }, 
	{ label: '快递面单', value: 0 }
]

const stateOptions = [
  { label: '待执行', value: 0 },
  { label: '执行中', value: 1 },
  { label: '已完成', value: 2 },
  { label: '执行失败', value: 3 },
]

//表单配置项
const formItemData = {
  'printTime': {
    itemConfig: {
      name: 'printTime'
    },
    elConfig: {
      formType: 'rangTime',
      placeholder: ['批次打印开始时间', '批次打印结束时间'],
      prefix: '批次打印时间',
    }
  },
  'systemNos': {
    itemConfig: {
      name: 'systemNos'
    },
    elConfig: {
      formType: 'Input',
      placeholder: '系统订单号',
    }
  },
  'orderSn': {
    itemConfig: {
      name: 'orderSn'
    },
    elConfig: {
      formType: 'Input',
      placeholder: '出库单号',
    }
  },
  'logisticsNo': {
    itemConfig: {
      name: 'logisticsNo'
    },
    elConfig: {
      formType: 'Input',
      placeholder: '快递单号',
    }
  },
  'userId': {
    itemConfig: {
      name: 'userId'
    },
    elConfig: {
      formType: 'SearchSelect',
      placeholder: '操作人',
      fetchOptions: handleRefundUserList,
    }
  },
  'printerName': {
    itemConfig: {
      name: 'printerName'
    },
    elConfig: {
      formType: 'Input',
      placeholder: '打印机名称',
    }
  },
  'warehouseId': {
    itemConfig: {
      name: 'warehouseId'
    },
    elConfig: {
      formType: 'Select',
      placeholder: '仓库',
      options: stateOptions
    }
  },
  'printType': {
    itemConfig: {
      name: 'printType'
    },
    elConfig: {
      formType: 'Select',
      placeholder: '打印类型',
      options: printTypeOptions
    }
  },

}

//获取操作人列表
function handleRefundUserList(username) {
  return new Promise(async (resolve, reject) => {
    const { data, success } = await queryOperationList({ 
      username,
      current: 1,
      size: 20,
    });
    if (success) {
      const newArr = [];
      data.map(item => {
        newArr.push({
          label: item.name ? `${item.name}（${item.account}）` : item.account,
          value: item.id,
        })
      })
      resolve(newArr);
    } else {
      reject();
    }
  })
}

//处理表单数据
export const getFormItem = (optionsData, type) => {
  const arr = [];
  Object.keys(formItemData).map(v => {
    if(!['0', '1', '2'].includes(type) && v == 'warehouseId') {
      return;
    }
    if(type != '3' && v == 'printType') {
      return;
    }
    if(optionsData && optionsData[v]) {
      formItemData[v].elConfig.options = optionsData[v];
    }
    arr.push(formItemData[v]);
  })
  return arr;
}

//tab源数据
export const tabArr = [
	{ key: '0', tab: '打单发货' },
	{ key: '1', tab: '销售出库单' },
	{ key: '2', tab: '分拣框' },
	{ key: '3', tab: '到货清点' },
	{ key: '4', tab: '快销采购' },
	{ key: '5', tab: '快销退货' },
]